package LeetCode;

public class LC_106_ConstructBinaryTreefromInorderandPostorderTraversal {
    public static void main(String[] args) {

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            if (inorder == null || postorder == null || inorder.length == 0 || inorder.length != postorder.length)
                return null;
            return solve(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
        }

        private TreeNode solve(int[] inorder, int x, int y, int[] postorder, int i, int j) {
            if (x >= 0 && x <= y && i >= 0 && i <= j) {
                if (x == y)
                    return new TreeNode(postorder[j]);
                else if (x < y) {
                    TreeNode root = new TreeNode(postorder[j]);
                    int idx = x;
                    while (idx < y && inorder[idx] != postorder[j]) idx++;

                    int leftLength = idx - x;
                    if (leftLength > 0) root.left = solve(inorder, x, idx - 1, postorder, i, i + leftLength - 1);
                    int rightLength = y - idx;
                    if (rightLength > 0)
                        root.right = solve(inorder, idx + 1, y, postorder, i + leftLength, j - 1);
                    return root;
                } else
                    return null;
            }
            return null;
        }
    }
}
